package exer;

/**
 * 删除重复元素，使每个元素只出现一次
 *
 * @author shkstart
 * @create 2022-08-13-0:11
 */
public class Num83 {
    public ListNode deleteDuplicates(ListNode head) {

        ListNode dummyHead = new ListNode(101);
        dummyHead.next = head;
        ListNode perv = dummyHead;
        ListNode cur = perv.next;


        while (cur != null) {
            ListNode next = cur.next;
            //此时只有一个元素，不存在重复
            if(cur.next==null){
                return dummyHead.next;
            }

//            存在相同元素
            if (cur.val == next.val) {
//                next一直向后寻找相同点元素直到找到不同的为止
                while (cur.val == next.val) {
                    next=next.next;
                }
//                找到不同的元素，对相同的元素next进行删除
                perv.next = next;
                cur = perv.next;

            }else{
                cur = cur.next;
                perv=perv.next;

            }
        }
        return dummyHead.next;


    }

}
